//
//  BJInvestmentGoodsButton.m
//  SZBank
//
//  Created by rimi on 16/7/21.
//  Copyright © 2016年 LuoYao. All rights reserved.
//

#import "BJInvestmentGoodsButton.h"
#import "BJGoodsImageView.h"
@interface BJInvestmentGoodsButton ()
{
    NSMutableArray *subDataArray;/**< 子页面数据 */
    NSMutableDictionary *subDataIdDataDictionary;/**< 子页面ID */
}
@end

@implementation BJInvestmentGoodsButton

- (instancetype)initWithFrame:(CGRect)frame withDataDictionary:(NSDictionary *)dataDictionary orginDataArray:(NSArray *)orginDataArray
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setBackgroundImage:[LocalDataManager getImageWithSourceOfPath:@"理财_cell背景"] forState:UIControlStateNormal];
        //子页面数据
        subDataArray = [NSMutableArray array];
        subDataIdDataDictionary = [NSMutableDictionary dictionary];
     
        subDataArray = [orginDataArray mutableCopy];
        subDataIdDataDictionary = [dataDictionary mutableCopy];
        //标题label
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height * 0.16)];
        label.textAlignment = NSTextAlignmentCenter;
        label.font = [UIFont systemFontOfSize:18.0];
        label.text = [NSString stringWithFormat:@"【%@】",dataDictionary[@"name"]];
        label.backgroundColor = [UIColor clearColor];
        [self addSubview:label];
        
        //图片
        UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(self.frame.size.width * 0.1, CGRectGetMaxY(label.frame) + self.frame.size.height * 0.1 , self.frame.size.width * 0.8, self.frame.size.height - label.frame.size.height * 3)];
        imageView.image = [LocalDataManager getImageWithURL:dataDictionary[@"image"]];
        imageView.layer.borderWidth = self.frame.size.width * 0.006;
        [self addSubview:imageView];
        
        //添加点击事件
        [self addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];

    }
    return self;
}


#pragma maek -- actions
- (void)clickButton:(UIButton *)sender{
    NSDictionary *dictionary = [NSDictionary dictionary];
    for (NSDictionary *dict  in subDataArray) {
        if ([dict[@"ID"] isEqualToString:subDataIdDataDictionary[@"id"]]) {
            dictionary = dict;
        }
    }


    BJGoodsImageView *goods = [[BJGoodsImageView alloc]initWithFrame:CGRectMake(0, KHEIGHT * 0.1, KWIDTH , KHEIGHT * 0.88) withDataDictionary:dictionary];
    
    [[UIApplication sharedApplication].keyWindow.rootViewController.view addSubview:goods];
   
}

@end
